package cn.zhang.reimbursement_1.service.impl;

import cn.zhang.reimbursement_1.common.CommonMethod;
import cn.zhang.reimbursement_1.config.LoginInterceptor;
import cn.zhang.reimbursement_1.mapper.RecordMapper;
import cn.zhang.reimbursement_1.model.Record;
import cn.zhang.reimbursement_1.request.LoginRequest;
import cn.zhang.reimbursement_1.tools.JsonData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhang.reimbursement_1.model.Preject;
import cn.zhang.reimbursement_1.service.PrejectService;
import cn.zhang.reimbursement_1.mapper.PrejectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author zfs90
* @description 针对表【t_preject】的数据库操作Service实现
* @createDate 2022-03-25 15:45:16
*/
@Service
public class PrejectServiceImpl implements PrejectService{

    @Autowired
    private PrejectMapper prejectMapper;
    @Autowired
    private RecordMapper recordMapper;


    /**
     * 添加项目类型
     * @param preject
     * @return
     */
    @Override
    public JsonData savePreject(Preject preject) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        if (CommonMethod.checkRole(loginRequest)){
            prejectMapper.insert(preject);
            return JsonData.buildSuccess();

        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }

    /**
     * 查询项目类型
     * @return
     */
    @Override
    public JsonData getPreject() {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
            List<Preject> prejects = prejectMapper.selectList(new QueryWrapper<Preject>());
            return JsonData.buildSuccess(prejects);


    }

    /**
     * 修改项目类型
     * @param preject
     * @return
     */
    @Override
    public JsonData updatePreject(Preject preject) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        Record record = new Record();

        if (CommonMethod.checkRole(loginRequest)){
            prejectMapper.updateById(preject);

            record.setRealname(loginRequest.getUsername());
            record.setOperation("修改");
            record.setInsertTime(new Date());
            record.setDetails("修改了"+preject.getPreject());
            recordMapper.insert(record);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }

    /**
     * 删除项目类型
     * @param delId
     * @return
     */
    @Override
    public JsonData delPreject(Integer delId) {
        LoginRequest loginRequest  = LoginInterceptor.loginThreadLocal.get();
        Preject preject = prejectMapper.selectById(delId);
        Record record = new Record();

        if (CommonMethod.checkRole(loginRequest)){
            prejectMapper.deleteById(delId);
            record.setRealname(loginRequest.getUsername());
            record.setOperation("删除");
            record.setInsertTime(new Date());
            record.setDetails("删除了"+preject.getPreject());
            recordMapper.insert(record);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }
}




